package com.kky.pp.activity.match

import android.os.Handler
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.kky.pp.R
import com.kky.pp.util.startAnimate
import com.kky.pp.util.SoftInputUtils
import com.custom.base.mvp.MVPBaseActivity
import com.custom.base.util.ToastUtil
import com.kky.pp.databinding.ActivityMatchedBinding
import com.kky.pp.util.KeyboardUtils


class MatchedActivity : MVPBaseActivity<MatchedContract.View, MatchedPresenter,ActivityMatchedBinding>(ActivityMatchedBinding::inflate),
    MatchedContract.View {
    private val keyboardUtils by lazy { KeyboardUtils() }
    override fun setFullScreen(): Boolean {
        return true
    }

    override fun bindLayout(): Int {
        return R.layout.activity_matched
    }


    private fun initAnimate() {
    }

    override fun initView() {
        initAnimate()
        conMatchSuccessfulContainer.startAnimate(frameMatchSuccessfulOwn,frameMatchSuccessfulLike,imgViewMatchSuccessfulClose,imageViewMatchSuccessfulHeart)

        Handler().postDelayed(Runnable {
            imgViewMatchSuccessfulClose.visibility = View.VISIBLE
        }, 70)

        Handler().postDelayed(Runnable {
            editMatchSuccessful.isFocusable = true
        }, 700)
        imgViewMatchSuccessfulClose.setOnClickListener {
            onBackPressed()
        }

        setTextAnim()
        txtMatchSuccessfulSend.setOnClickListener {
            val content = editMatchSuccessful.text.toString()
            if (content.isNullOrEmpty()){
                ToastUtil.toast(this@MatchedActivity.getString(R.string.cannot_input_all_spaces))
                return@setOnClickListener
            }else{
                mPresenter?.tryToSend(content)
            }
        }
        keyboardUtils.registerKeyboardHeightListener(this, listener = object: KeyboardUtils.KeyboardHeightListener{
            override fun onKeyboardHeightChanged(height: Int) {
                val translationY = if (height != 0) {
                    (height - conMatchSuccessfulContainer.height)
                } else {
                    0
                }

                conMatchSuccessfulContainer.setPadding(0,0,0, translationY)
            }

        })
    }

    private fun setTextAnim() {
        txtMatchSuccessfulEmojiWave.text = String(Character.toChars(0x1F44B))
        txtMatchSuccessfulEmojiWave.setOnClickListener {
            txtMatchSuccessfulEmojiAnim.text = String(Character.toChars(0x1F44B))
            val animationEnlarge: Animation =
                AnimationUtils.loadAnimation(mActivity, R.anim.anim_small_400)
            txtMatchSuccessfulEmojiAnim.startAnimation(animationEnlarge)
            Handler().postDelayed(Runnable {
                mPresenter?.tryToSend(txtMatchSuccessfulEmojiAnim.text.toString())
            }, 500)
        }
        txtMatchSuccessfulEmojiWinkingFace.text = String(Character.toChars(0x1F339))
        txtMatchSuccessfulEmojiWinkingFace.setOnClickListener {
            txtMatchSuccessfulEmojiAnim.text = String(Character.toChars(0x1F339))
            val animationEnlarge: Animation =
                AnimationUtils.loadAnimation(mActivity, R.anim.anim_small_400)
            txtMatchSuccessfulEmojiAnim.startAnimation(animationEnlarge)
            Handler().postDelayed(Runnable {
                mPresenter?.tryToSend(txtMatchSuccessfulEmojiAnim.text.toString())
            }, 500)
        }
        txtMatchSuccessfulEmojiHeart.text = String(Character.toChars(0x2764))
        txtMatchSuccessfulEmojiHeart.setOnClickListener {
            txtMatchSuccessfulEmojiAnim.text = String(Character.toChars(0x2764))
            val animationEnlarge: Animation =
                AnimationUtils.loadAnimation(mActivity, R.anim.anim_small_400)
            txtMatchSuccessfulEmojiAnim.startAnimation(animationEnlarge)
            Handler().postDelayed(Runnable {
                mPresenter?.tryToSend(txtMatchSuccessfulEmojiAnim.text.toString())
            }, 500)
        }
        txtMatchSuccessfulEmojiLecherous.text = String(Character.toChars(0x1F60D))
        txtMatchSuccessfulEmojiLecherous.setOnClickListener {
            txtMatchSuccessfulEmojiAnim.text = String(Character.toChars(0x1F60D))
            val animationEnlarge: Animation =
                AnimationUtils.loadAnimation(mActivity, R.anim.anim_small_400)
            txtMatchSuccessfulEmojiAnim.startAnimation(animationEnlarge)
            Handler().postDelayed(Runnable {
                mPresenter?.tryToSend(txtMatchSuccessfulEmojiAnim.text.toString())
            }, 500)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        keyboardUtils.unregisterKeyboardHeightListener(this@MatchedActivity)
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN) {
            val v = currentFocus
            if (v is EditText) {
                val l = intArrayOf(0, 0)
                v.getLocationInWindow(l)
                onTouchEditText(
                    ev.x > l[0]
                            && ev.x < l[0] + v.getWidth()
                            && ev.y > l[1]
                            && ev.y < l[1] + v.getHeight()
                )
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    private fun onTouchEditText(isTouchInEditText: Boolean) {
        if (!isTouchInEditText) {
            SoftInputUtils.hideSoftInput(editMatchSuccessful)
        }
    }


    override val imgViewMatchSuccessful: ImageView
        get() = viewBinding.imgViewMatchSuccess
    override val imgViewMatchSuccessfulClose: ImageView
        get() = viewBinding.imgViewMatchSuccessClose
    override val txtMatchSuccessfulEmojiWave: TextView
        get() = viewBinding.txtMatchSuccessEmojiWave
    override val txtMatchSuccessfulEmojiWinkingFace: TextView
        get() = viewBinding.txtMatchSuccessEmojiWinkingFace
    override val txtMatchSuccessfulEmojiHeart: TextView
        get() = viewBinding.txtMatchSuccessEmojiHeart
    override val txtMatchSuccessfulEmojiLecherous: TextView
        get() = viewBinding.txtMatchSuccessEmojiLecherous
    override val editMatchSuccessful: EditText
        get() = viewBinding.editMatchSuccess

    override val txtMatchSuccessfulSend: TextView
        get() = viewBinding.txtMatchSuccessSend
    override val txtMatchSuccessfulEmojiAnim: TextView
        get() = viewBinding.txtMatchSuccessEmojiAnim
    override val conMatchSuccessfulContainer: ConstraintLayout
        get() = viewBinding.conMatchSuccessContainer
    override val frameMatchSuccessfulOwn: FrameLayout
        get() = viewBinding.frameMatchSuccessOwn
    override val frameMatchSuccessfulLike: FrameLayout
        get() = viewBinding.frameMatchSuccessLike
    override val imageViewMatchSuccessfulOwn: ImageView
        get() = viewBinding.imageViewMatchSuccessOwn
    override val imageViewMatchSuccessfulLike: ImageView
        get() = viewBinding.imageViewMatchSuccessLike
    override val imageViewMatchSuccessfulHeart: ImageView
        get() = viewBinding.imageViewMatchSuccessHeart

}